package aliyun.cx.admin.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * 密码工具类
 */
public class PasswordUtil {
    
    private static final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
    
    /**
     * 加密密码
     */
    public static String encode(String rawPassword) {
        return passwordEncoder.encode(rawPassword);
    }
    
    /**
     * 验证密码
     */
    public static boolean matches(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
    
    /**
     * 生成指定密码的BCrypt编码
     * 用于生成数据库中的密码
     */
    public static void main(String[] args) {
        String password = "123456";
        String specifiedEncoded = "$2a$10$EyQmCNB5oHt9F.cHDRwQM.LfRUMZBuwU4U91ahCCcLrb/Bdm5W/qq";
        
        System.out.println("密码: " + password);
        System.out.println("指定的BCrypt编码: " + specifiedEncoded);
        System.out.println("验证结果: " + matches(password, specifiedEncoded));
        
        // 生成新的编码
        String newEncoded = encode(password);
        System.out.println("新生成的BCrypt编码: " + newEncoded);
        System.out.println("新编码验证结果: " + matches(password, newEncoded));
    }
}
